#!/bin/bash
# Script to install chaincode onto a peer node
{% include "envsetting.j2" %}
cd /go/src/github.com/chaincode/{{ CC_NAME }}

{% if fabric.goproxy is defined and CC_LANGUAGE == 'go' %}
  go env -w GOPROXY={{ fabric.goproxy }}
{% endif %}

if [ ! -f "{{ CC_NAME }}_{{ CC_LANGUAGE}}_{{ CC_VERSION }}.tar.gz" ]; then
{% if CC_LANGUAGE == 'go' %}
  cd {{ CC_LANGUAGE }}
  GO111MODULE=on
  go mod vendor
  cd -
  peer lifecycle chaincode package {{ CC_NAME }}_{{ CC_LANGUAGE }}_{{ CC_VERSION }}.tar.gz \
    -p /go/src/github.com/chaincode/{{ CC_NAME}}/{{ CC_LANGUAGE }}/ \
    --lang golang --label {{ CC_NAME }}_{{ CC_VERSION }}
{% else %}
  peer lifecycle chaincode package {{ CC_NAME }}_{{ CC_LANGUAGE }}_{{ CC_VERSION }}.tar.gz \
    -p /go/src/github.com/chaincode/{{ CC_NAME}}/{{ CC_LANGUAGE }}/ \
    --lang {{ CC_LANGUAGE }} --label {{ CC_NAME }}_{{ CC_VERSION }}
{% endif %}
fi

peer lifecycle chaincode install {{ CC_NAME}}_{{ CC_LANGUAGE }}_{{ CC_VERSION }}.tar.gz
